
const moment = require('moment');
const { dataValid } = require('./utils/util.js');


import Report from './report/Report';

class Decoder {


  prepareData = (data) => {

    console.log('数据有进来吗?????')

    const measure = {};

    measure.gender = data.gender;
    measure.height = data.height;
    measure.birthday = moment(data.birthday,"YYYY-M-D");
    measure.stature = data.stature
    measure.weight = parseFloat(parseFloat(data.weight).toFixed(2));
    measure.bmi = parseFloat(parseFloat(data.bmi).toFixed(1));
    measure.bodyfat = parseFloat(parseFloat(data.bodyfat).toFixed(1));

    if(!measure.stature){
      measure.stature = data.internal_model == '0004' ? data.height : 0
    }
    if (dataValid(data)) {

      measure.water = data.water ? parseFloat(parseFloat(data.water).toFixed(1)) : 0;
      measure.subfat = data.subfat ? parseFloat(parseFloat(data.subfat).toFixed(1)) : 0;
      measure.muscleMass = data.sinew ? parseFloat(parseFloat(data.sinew).toFixed(2)) : 0;
      measure.muscle = data.muscle ? parseFloat(parseFloat(data.muscle).toFixed(1)) : 0;
      measure.bone = data.bone ? parseFloat((parseFloat(data.bone)).toFixed(2)) : 0;
      measure.bmr = data.bmr ? parseFloat((parseFloat(data.bmr)).toFixed()) : 0;
      measure.protein = data.protein ? parseFloat(parseFloat(data.protein).toFixed(1)) : 0;
      measure.lbm = data.fat_free_weight ? parseFloat(parseFloat(data.fat_free_weight).toFixed(2)) : 0;
      measure.visfat = data.visfat ? parseInt(parseInt(data.visfat).toFixed(0)) : 0;

      measure.heartRate = data.heart_rate ? parseInt(parseInt(data.heart_rate).toFixed(0)) : 0;
      measure.heartIndex = data.cardiac_index ? parseInt(parseInt(data.cardiac_index).toFixed(0)) : 0;

      measure.score = data.score || 0;

      measure.bodyAge = data.bodyage;
      measure.bodyShape = data.body_shape
    } else {
      measure.water = 0;
      measure.subfat = 0;
      measure.muscleMass = 0;
      measure.muscle = 0;
      measure.bone = 0;
      measure.bmr = 0;
      measure.protein = 0;
      measure.lbm = 0;
      measure.visfat = 0;

      measure.score = 0;

      measure.bodyAge = 0;

      measure.bodyShape = 0;

      measure.heartRate = 0

    }

    measure.time = new moment(data.local_updated_at, "YYYY-MM-DD HH:mm:ss");
    measure.measure_time = measure.time;

    measure.origin = data;
    return new Report(measure)
  };

  constructor(obj) {

    const data = obj;

    const report = this.prepareData(data);
    //console.log("整合报告", report)

    return report;
  }

}

export default Decoder;



// WEBPACK FOOTER //
// ./src/ble/decoder.js
